﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HumanResorcesProj.BLL;
using System.Security.Cryptography;
using System.Text;

namespace HumanResorcesProj.GUI.Login
{
    public partial class Login : System.Web.UI.Page
    {

        string[] roles = null;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public static string HashPassword(string UnHashedPassword)
        {
            return Convert.ToBase64String(new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(UnHashedPassword)));
        }


        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            bool Authenticated = false;
         
            Authenticated = SiteSpecificAuthenticationMethod(Log.UserName,HashPassword(Log.Password));
            
            e.Authenticated = Authenticated;
        }

        private bool IsAdministrator(string UserType)
        {
            HumanResorcesProj.BLL.Login userBLL = new HumanResorcesProj.BLL.Login();
            string UserTypeNum = userBLL.GetDataByLogName("Admin")[0].UserName;
            if (UserTypeNum == UserType) return true;

            return false;

        }

        private bool SiteSpecificAuthenticationMethod(string UserName ,string Password )
        {


           // if (UserName.Equals("admin"))
            //{
               // Log.DestinationPageUrl = "~/GUI/login/Register.aspx";
                //return true;
           // }
            try
            {
                HumanResorcesProj.BLL.Login userBLL = new HumanResorcesProj.BLL.Login();
                if (userBLL.GetDataByLogName(UserName).Count < 1)
                    return false;
                HumanResorcesProj.DAL._HR_DS.LoginRow loginUserRow = userBLL.GetDataByLogName(UserName)[0];
                if (Password.Equals(loginUserRow.Password))
                {
                    if (IsAdministrator(loginUserRow.UserName))
                    {
                        Log.DestinationPageUrl = "~/GUI/login/Register.aspx";
                        roles = new string[1];
                        roles[0] = "admin";
                    }
                    else
                    {
                        Log.DestinationPageUrl = "~/GUI/MainWindow.aspx";
                        roles = new string[1];
                        roles[0] = "user";
                    }
                    return true;
                }
                return false;
            }
            //TODO dispose here
            finally
            {

            }
            /*
            HumanResorcesProj.BLL.Login BLL = new HumanResorcesProj.BLL.Login();
            HumanResorcesProj.DAL._HR_DS.LoginDataTable Table =  BLL.GetData();
            string Pass = null;
            bool UserFound = false;
            int rows = Table.Rows.Count;
            for (int i = 0; i < rows; i++)
            {
                if (UserName.Equals(Convert.ToString(Table[i][0])))
                {
                    UserFound = true;
                    Pass = Convert.ToString(Table[i][1]);
                }
            }

            if (UserFound)
            {
                if (Password.Equals(Pass))
                {
                    if (UserName.Equals("Admin")) Log.DestinationPageUrl = "~/GUI/Login/WebForm1.aspx";
                         else Log.DestinationPageUrl = "~/GUI/MainWindow.aspx";
                    return true;
                }
            }
                else return false;
            return false;
            */
        }
    }
}
